package com.study.jyg.rxjava2study.activity.design;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.base.BasicActivity;
import com.study.jyg.rxjava2study.utils.AdapterInit;
import com.study.jyg.rxjava2study.utils.dialog.DialogUtils;
import com.study.jyg.rxjava2study.widget.MyRecyclerView;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * bottomsheet 效果的两种实现
 * 原文链接：https://www.jianshu.com/p/ae866a43c8e3
 * create by 2019-03-25 with jyg
 */
public class BottomSheetActivity extends BasicActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.btn1)
    Button btn1;
    @BindView(R.id.btn2)
    Button btn2;
    @BindView(R.id.recyclerView)
    MyRecyclerView recyclerView;
    @BindView(R.id.design_bottom_sheet)
    NestedScrollView designBottomSheet;

    private BottomSheetBehavior behavior;
    private BaseQuickAdapter<String, BaseViewHolder> adapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_bottom_sheet;
    }

    @Override
    protected void initView() {
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(v -> finish());

        initAdapter();

        behavior = BottomSheetBehavior.from(designBottomSheet);
        behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
        behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View view, int i) {
                //这里是bottomSheet 状态的改变
                Log.e("behavior", "onStateChanged:i="+ i);
            }

            @Override
            public void onSlide(@NonNull View view, float v) {
                //这里是拖拽中的回调，根据slideOffset可以做一些动画
                Log.e("behavior", "onStateChanged:v="+ v);
            }
        });
        btn1.setOnClickListener(v -> showBottomSheet());
        btn2.setOnClickListener(v -> DialogUtils.bottomDialog(mContext, R.layout.item_recyclerview_textview, bottomSheetDialog -> {
            TextView tvItem = bottomSheetDialog.findViewById(R.id.tvItem);
            tvItem.setText("使用dialoguitis的bottomdialog");
        }));
    }

    private void initAdapter(){
        adapter = AdapterInit.initRecyclerView( recyclerView, R.layout.item_recyclerview_textview, Arrays.asList(new String[25]), new LinearLayoutManager(mContext), (helper, item) -> {
            helper.setText(R.id.tvItem, "https://www.jianshu.com/p/488283f74e69");

        });
    }

    /**
     * 打开bottomsheet效果
     */
    public void showBottomSheet() {
//        behavior.setState(BottomSheetBehavior.STATE_HALF_EXPANDED);//打开一半
//        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);//全部展开
        behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);//打开到（折叠）状态的时候bottom sheet残留的高度 需要设置app:behavior_peekHeight
//        behavior.setState(BottomSheetBehavior.STATE_DRAGGING);//拖延的；拖曳用的 (不知道怎么用， 报非法状态参数)
//        behavior.setState(BottomSheetBehavior.STATE_SETTLING);//固定(不知道怎么用， 报非法状态参数)

    }

}
